package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.system.template.LGoodsTemplate;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.LGoods;
import com.ruoyi.system.service.ILGoodsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 货品资料Controller
 *
 * @author zyb
 * @date 2022-01-02
 */
@RestController
@RequestMapping("/system/goods")
public class LGoodsController extends BaseController
{
    @Autowired
    private ILGoodsService lGoodsService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 查询货品资料列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:list')")
    @GetMapping("/list")
    public TableDataInfo list(LGoods lGoods)
    {
        startPage();
        List<LGoods> list = lGoodsService.selectLGoodsList(lGoods);
        return getDataTable(list);
    }

    /**
     * 导出货品资料列表
     */
    @PreAuthorize("@ss.hasPermi('system:goods:export')")
    @Log(title = "货品资料", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LGoods lGoods)
    {
        List<LGoods> list = lGoodsService.selectLGoodsList(lGoods);
        ExcelUtil<LGoods> util = new ExcelUtil<LGoods>(LGoods.class);
        util.exportExcel(response, list, "货品资料数据");
    }

    @Log(title = "货品导入", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('system:goods:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<LGoods> util = new ExcelUtil<LGoods>(LGoods.class);
        List<LGoods> LGoodsList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = lGoodsService.importGoods(LGoodsList, updateSupport);
        return AjaxResult.success(message);
    }

    /**
     * 获取货品资料详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:goods:query')")
    @GetMapping(value = "/{goodsid}")
    public AjaxResult getInfo(@PathVariable("goodsid") Long goodsid)
    {
        return AjaxResult.success(lGoodsService.selectLGoodsByGoodsid(goodsid));
    }

    /**
     * 新增货品资料
     */
    @PreAuthorize("@ss.hasPermi('system:goods:add')")
    @Log(title = "货品资料", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LGoods lGoods)
    {
        redisTemplate.opsForList().leftPop("lGoods");
        return toAjax(lGoodsService.insertLGoods(lGoods));
    }

    /**
     * 修改货品资料
     */
    @PreAuthorize("@ss.hasPermi('system:goods:edit')")
    @Log(title = "货品资料", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LGoods lGoods)
    {
        redisTemplate.opsForList().leftPop("lGoods");
        return toAjax(lGoodsService.updateLGoods(lGoods));
    }

    /**
     * 删除货品资料
     */
    @PreAuthorize("@ss.hasPermi('system:goods:remove')")
    @Log(title = "货品资料", businessType = BusinessType.DELETE)
	@DeleteMapping("/{goodsids}")
    public AjaxResult remove(@PathVariable Long[] goodsids)
    {
        redisTemplate.opsForList().leftPop("lGoods");
        return toAjax(lGoodsService.deleteLGoodsByGoodsids(goodsids));
    }

    @GetMapping("/list/all")
    public AjaxResult listAll()
    {
        List<LGoods> list = (List<LGoods>) redisTemplate.opsForList().range("lGoods",0,-1);
        if(list.size()==0){
            LGoods lGoods=new LGoods();
            lGoods.setClosed(0L);
            list = lGoodsService.selectLGoodsList(lGoods);
            redisTemplate.opsForList().leftPushAll("lGoods",list);
        }

        return AjaxResult.success(list);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<LGoodsTemplate> util = new ExcelUtil<LGoodsTemplate>(LGoodsTemplate.class);
        util.importTemplateExcel(response, "货品数据");
    }
}
